<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">SKU库存查询</p>
    </div>
    <div class="view-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="排序" prop="searchValue">
          <el-select
            v-model="queryParams.searchValue"
            clearable
            size="small"
            style="width: 200px;"
            @change="handleQuery"
          >
            <el-option
              v-for="item in searchValue"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="仓库" prop="warehouseName">
          <el-select
            v-model="queryParams.warehouseName"
            clearable
            size="small"
            style="width: 200px;"
            @change="val => warehouseData(val)"
          >
            <el-option
              v-for="item in warehouseList"
              :key="item.name"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="SKU名称" prop="specificationName">
          <el-input
            v-model="queryParams.specificationName"
            placeholder="请输入SKU名称"
            clearable
            size="small"
            style="width:230px;"
            @keyup.enter.native="handleQuery"
            @clear="handleClear"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="23">
          <el-button
            class="btn-scanCodeDel"
            type="primary"
            icon="el-icon-download"
            size="mini"
            @click="btnExport()"
          >导出</el-button>
        </el-col>
        <el-col :span="1">
          <el-popover placement="bottom" title="筛选列" trigger="click" width="40">
            <el-checkbox-group v-model="checkedColumns" size="mini">
              <el-checkbox v-for="item in checkBoxGroup" :key="item" :label="item" :value="item"></el-checkbox>
            </el-checkbox-group>
            <div title="筛选列" class="filter-table-col" slot="reference">
              <img src="@/assets/img/screening.png" width="25" />
            </div>
          </el-popover>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" border>
        <el-table-column type="selection" width="35" align="center" />
        <el-table-column label="序号" type="index" align="center" width="50">
           <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <template v-for="(item, index) in tableHeaderData">
          <el-table-column
            :label="item.label"
            :prop="item.prop"
            :align="item.align"
            :sortable="item.sortable"
            :show-overflow-tooltip="true"
            :key="index"
            :width="item.width"
            v-if="colData[index].isTrue"
          >
            <template slot-scope="scope">
              <span>{{ scope.row[scope.column.property] }}</span>
            </template>
          </el-table-column>
        </template>
        <!-- <el-table-column label="SKU编码" align="center" prop="specificationCode" :show-overflow-tooltip="true" width="180"/>
        <el-table-column label="SKU名称" align="center" prop="specificationName" :show-overflow-tooltip="true" />
        <el-table-column label="规格" align="center" prop="productSpace" :show-overflow-tooltip="true" width="180"/>
        <el-table-column label="批次号" align="center" prop="batchNumber" :show-overflow-tooltip="true" width="150"/>
        <el-table-column label="仓库" align="center" prop="warehouseName" :show-overflow-tooltip="true" width="180"/>
        <el-table-column label="库存数量" align="center" prop="store" :show-overflow-tooltip="true" width="100" />
        <el-table-column label="计量单位" align="center" prop="productUnit" :show-overflow-tooltip="true" width="100" />
        <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" width="180"/> -->
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { skuListStock } from "@/api/system/stock/stock"
import { queryWarehouse } from "@/api/system/warehouse/warehouse"
import { exportSku } from "@/api/system/export/export"

export default {
  name: "stock",
  data() {
    return {
      // 遮罩层
      loading: true,
      total: 0,
      list: [],
      // 排序
      searchValue:[
        {
          value: '1',
          label: '同一产品按数量由大到小'
        },
        {
          value: '2',
          label: '同一产品按数量由小到大'
        },
      ],
      warehouseList: [], // 仓库列表
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: '1',
        warehouseName: undefined,
        warehouseId: undefined,
        specificationName: undefined
      },
      ids: [],
      tableHeaderData: [
        { label: "SKU编码", align: "center", prop: "specificationCode", width: "150"},
        { label: "SKU名称", align: "center", prop: 'specificationName', width: "150" },
        { label: "规格", align: "center", prop: "productSpace", width: "150"},
        { label: "批次号", align: "center", prop: "batchNumber", width: "120"},
        { label: "仓库", align: "center", prop: "warehouseName", width: "120"},
        { label: "库存数量", align: "center", prop: "store", width: "100" },
        { label: "计量单位", align: "center", prop: "productUnit", width: "100"},
        { label: "备注", align: "center", prop: "remark"},
      ],
      colData: [
        { title: "SKU编码", isTrue: true },
        { title: "SKU名称", isTrue: true },
        { title: "规格", isTrue: true },
        { title: "批次号", isTrue: true },
        { title: "仓库", isTrue: true },
        { title: "库存数量", isTrue: true },
        { title: "计量单位", isTrue: true },
        { title: "备注", isTrue: true },
      ],
      // 当前选中的多选框，代表当前展示的列
      checkedColumns: ["SKU编码", "SKU名称", "规格", "批次号", "仓库", "库存数量", "计量单位", "备注"],
      // 多选框的列表，列出表格的每一列
      checkBoxGroup: ["SKU编码", "SKU名称", "规格", "批次号", "仓库", "库存数量", "计量单位", "备注"]
    }
  },
  created() {
    this.getList()
    this.getWarehouseList()
  },
  watch: {
    checkedColumns(val) {
      let arr = this.checkBoxGroup.filter(i => !val.includes(i));
      this.colData.filter(i => {
        if (arr.indexOf(i.title) != -1) {
          i.isTrue = false;
        } else {
          i.isTrue = true;
        }
      });
      this.reload = Math.random()
    }
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true
      skuListStock(this.addDateRange(this.queryParams)).then((res) => {
        this.list = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    // 获取仓库列表
    getWarehouseList(){
      queryWarehouse().then(res => {
        if(res.code == 200){
          this.warehouseList = res.data
        }
      })
    },
    warehouseData(val){
      if(val){
        let data = this.warehouseList.find(item => item.name == val)
        this.queryParams.warehouseName = data.name
        this.queryParams.warehouseId = data.warehouseId
      }else{
        this.queryParams.warehouseName = undefined
        this.queryParams.warehouseId = undefined
      }
      this.handleQuery()
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮
    resetQuery() {
      this.queryParams.warehouseId = undefined
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 清除
    handleClear(){
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(row) {
      this.ids = row.map(item => item.specificationId)
    },
    // 导出
    btnExport(){
      let a = this.colData.filter(i => {
        return i.isTrue == true
      })
      let b = []
      a.forEach((item) => {
        this.tableHeaderData.forEach((data) => {
          if(item.title == data.label){
            b.push({name: data.prop, value: item.title})
          }
        })
      })
      const obj = b.reduce((result, item) => {
        result[item.name] = item.value
        return result
      }, {})
      if(this.ids.length > 0){
        let data = {
          ids: this.ids,
          headMap: obj,
          searchValue: this.queryParams.searchValue,
          warehouseName: this.queryParams.warehouseName,
          warehouseId: this.queryParams.warehouseId
        }
        this.getExport(data)
      }else{
        let data = {
          headMap: obj,
          searchValue: this.queryParams.searchValue,
          warehouseName: this.queryParams.warehouseName,
          warehouseId: this.queryParams.warehouseId
        }
        this.getExport(data)
      }
    },
    getExport(data){
      this.$modal.loading("正在导出")
      exportSku(data).then(res => {
        this.$modal.closeLoading()
        let blob = new Blob([res], {
          type: res.type,
        })
        let downloadElement = document.createElement("a")
        let href = window.URL.createObjectURL(blob)
        downloadElement.href = href
        downloadElement.download = "SKU库存.xls";
        document.body.appendChild(downloadElement)
        downloadElement.click()
        window.URL.revokeObjectURL(href)
      })
    }
  }
}
</script>
